package com.myapp.ui;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.myapp.R;
import com.myapp.tools.AsyncImgLoader;
import com.myapp.tools.Tools;
import com.myapp.tools.AsyncImgLoader.ImageCallback;
import com.myapp.util.Constant;
import com.myapp.util.ScreenManager;
import com.myapp.widget.RoundImageView;

public class DiyDetailActivity extends Activity {

	private TextView title,content,toCourse,comment_msg;
	private ImageView img,head;
	private Bundle bundle = null;
	private AsyncImgLoader imgloader;
	private Button comment;
	private LinearLayout comment_ly;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_diydetail);
		imgloader = new AsyncImgLoader();
		bundle = getIntent().getExtras();
		comment_msg = (TextView)findViewById(R.id.diydetail_comment_msg);
		title = (TextView)findViewById(R.id.diydetail_title);
		content = (TextView)findViewById(R.id.diydetail_content);
		toCourse = (TextView)findViewById(R.id.diydetail_toCourse);
		comment_ly = (LinearLayout)findViewById(R.id.diydetail_comment);
		img = (ImageView)findViewById(R.id.diydetail_img);
		comment = (Button)findViewById(R.id.diydetail_comment_on);
		comment.setOnClickListener(new myClickListener());
		init();
		loadComment();
	}
	
	private void init(){
		title.setText(bundle.getString("name"));
		content.setText(bundle.getString("content"));
		imgloader.loadDrawable(this, bundle.getString("img"), new ImageCallback() {
			@Override
			public void imageLoaded(Drawable imageDrawable, String imageUrl) {
				if(imageDrawable!=null){
					img.setImageDrawable(imageDrawable);
				}
			}
		});
		toCourse.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(DiyDetailActivity.this,DiyCourseActivity.class);
				i.putExtras(bundle);
				startActivity(i);
			}
		});
	}
	
	private void loadComment(){
		AsyncTask<String, Integer, String> task = new AsyncTask<String, Integer, String>(){
			@Override
			protected String doInBackground(String... params) {
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("cid", getIntent().getExtras().getLong("did"));
				map.put("type", 1);
				return Tools.toNetWork("http://"+Constant.WS_IP+"/huyanqi-frontWar/rest/comment/findComments.json", map);
			}
			
			@Override
			protected void onPostExecute(String result) {
				super.onPostExecute(result);
				try {
					JSONObject jobj = new JSONObject(result).getJSONObject("commentListResp");
					if(jobj.getInt("infocode")==200){
						comment_ly.removeView(comment_msg);
						String data = jobj.getString("data");
						if(Tools.getJSONType(data)==0){
							JSONObject jdata = new JSONObject(data);
							setComment(jdata);
						}else{
							JSONArray jar = new JSONArray(data);
							for(int i=0;i<jar.length();i++){
								JSONObject jdata = (JSONObject) jar.get(i);
								setComment(jdata);
							}
						}
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		};
		task.execute("");
	}
	
	private class myClickListener implements OnClickListener{
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.diydetail_comment_on:
				Intent i = new Intent(DiyDetailActivity.this,CommentDiyActivity.class);
				Bundle b = new Bundle();
				b.putLong("did", bundle.getLong("did"));
				i.putExtras(b);
				startActivityForResult(i,1024);
				break;

			default:
				break;
			}
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode==1024){
			String result = data.getExtras().getString("result");
		}
	}
	
	private void setComment(JSONObject jdata){
		LinearLayout ll = new LinearLayout(this);
		ll.setGravity(Gravity.CENTER_VERTICAL);
		ll.setOrientation(LinearLayout.HORIZONTAL);
		final ImageView iv = new ImageView(this);
		ScreenManager.setMyLayoutParam(iv, 90, 90);
		iv.setPadding(10, 0, 0, 0);
		try {
			iv.setImageDrawable(imgloader.loadDrawable(this, "http://"+Constant.BACK_IP+"/head/"+jdata.getString("uHead"), new ImageCallback() {
				@Override
				public void imageLoaded(Drawable imageDrawable, String imageUrl) {
					if(imageDrawable!=null){
						iv.setImageDrawable(imageDrawable);
					}
				}
			}));
			TextView tv1 = new TextView(this);
			tv1.setText(jdata.getString("uNickname"));
			tv1.setPadding(10, 0, 0, 0);
			
			TextView tv2 = new TextView(this);
			tv2.setText(jdata.getString("cmContent"));
			tv2.setPadding(10, 0, 0, 0);
			
			final RoundImageView iv2 = new RoundImageView(this);
			
			iv2.setImageDrawable(imgloader.loadDrawable(this, "http://"+Constant.BACK_IP+"/comment/"+jdata.getString("cmImg"), new ImageCallback() {
				@Override
				public void imageLoaded(Drawable imageDrawable, String imageUrl) {
					if(imageDrawable!=null){
						iv2.setImageDrawable(imageDrawable);
					}
				}
			}));
			iv2.setPadding(0, 0, 10, 0);
			ScreenManager.setMyLayoutParam(iv2, 75, 75);
			
			LinearLayout ll2 = new LinearLayout(this);
			ScreenManager.setMyLayoutParam(ll2, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
			ll2.setGravity(Gravity.RIGHT);
			ll2.addView(iv2);

			ll.addView(iv);
			ll.addView(tv1);
			ll.addView(tv2);
			ll.addView(ll2);
			
			comment_ly.addView(ll);
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
}
